﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL;
using DAL;
using System.Collections.Generic;
using Telerik.Web.UI;

public partial class Baiviet : System.Web.UI.Page
{
    THANHVIENBLL tvBll = new THANHVIENBLL();
    BAIVIETBLL bvBll = new BAIVIETBLL();
    COMMENTBLL cmeBll = new COMMENTBLL();
    TINHTRANGBVBLL ttbvBll = new TINHTRANGBVBLL();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["tenthanhvien"] == null) Response.Redirect("LogIn.aspx");
        if(!Page.IsPostBack){
            BAIVIET bv = bvBll.getBaivietWithIDBaiviet(Int32.Parse(Page.Request["mabv"]));
            Label1.Text = bv.tenbaiviet;
            THANHVIEN tv = tvBll.getThanhvienWithIDBaiviet(Int32.Parse(Request.Params["mabv"]));

            Label_tenttvietbai.Text = tv.tenthanhvien;
            Label_chucvuttvietbai.Text = tv.chucvu;
            lb_noidung.Text = bv.noidung;
            lb_time.Text = " (" + bv.ngaylap.ToLongDateString() + " - " + bv.ngaylap.ToLongTimeString() + " )";
            if (!string.IsNullOrEmpty(tv.hinh))
            {
                Image_thanhvienvietbai.ImageUrl = "~/Hoso/hinh/Avatar/" + tv.mathanhvien.ToString() + "/" + tv.hinh;
            }
            else {
                Image_thanhvienvietbai.ImageUrl = "~/Hoso/hinh/Avatar/no_avatar.png";
            }

            VAITRO vt = (VAITRO)Session["vaitroObject"];
            if (vt.tenvaitro.Equals("Khách"))
            {
                p_editor.Visible = false;
                p_right_chat.Visible = false;
                rad_editor_chat.Visible = false;
            }

            //
            if (bvBll.isBlockWithIDBaiviet(bv.mabaiviet))
            {
                LinkButton2.Visible = false;
                LinkButton3.Visible = true;

                p_editor.Visible = false;
                rad_editor_chat.Visible = false;
            }
            else {
                LinkButton2.Visible = true;
                LinkButton3.Visible = false;
            }

            //
            THANHVIEN tvVietbai = bvBll.getThanhvienWithIDBaiviet(bv.mabaiviet);
            THANHVIEN tvDangnhap = (THANHVIEN)Session["thanhvienObject"];
            if (tvVietbai.mathanhvien != tvDangnhap.mathanhvien) {
                LinkButton1.Visible = false;
                LinkButton2.Visible = false;
                LinkButton3.Visible = false;
            }

            if(!bvBll.checkThanhvienIsInGroupWithIDBaiviet(tvDangnhap, bv.mabaiviet)){
                rad_editor_chat.Visible = false;
                p_editor.Visible = false;
            }

            //count for field "xem" of baiviet
            int count = bv.xem;
            count += 1;
            bvBll.updateValueXemWithIDBaiviet(bv.mabaiviet,count );
        }
    }
    protected void Timer2_Tick(object sender, EventArgs e)
    {
        
    }

    protected void btn_editor_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(rad_editor_chat.Text))
        {
            THANHVIEN tv = (THANHVIEN)Session["thanhvienObject"];
            BAIVIET bv = (BAIVIET)bvBll.getBaivietWithIDBaiviet(Int32.Parse(Request.Params["mabv"]));
            cmeBll.insertComment(rad_editor_chat.Text, DateTime.Now, tv.mathanhvien, bv.mabaiviet);
            rad_editor_chat.Content = "";
        }
    }
    protected void btn_refesh_Click(object sender, EventArgs e)
    {
        rad_editor_chat.Content = "";
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        BAIVIET bv = bvBll.getBaivietWithIDBaiviet( Int32.Parse(Page.Request["mabv"]));
        bvBll.setTinhtrangBaiviet(bv.mabaiviet, ttbvBll.Block());
        Response.Redirect("Phumuc.aspx?macm=" + bv.maphumuc);
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        BAIVIET bv = bvBll.getBaivietWithIDBaiviet(Int32.Parse(Page.Request["mabv"]));
        bvBll.DeleteBaivietWithId(bv.mabaiviet);
        Response.Redirect("Phumuc.aspx?macm=" + bv.maphumuc);
    }

    protected void LinkButton3_Click1(object sender, EventArgs e)
    {
        BAIVIET bv = bvBll.getBaivietWithIDBaiviet(Int32.Parse(Page.Request["mabv"]));
        bvBll.setTinhtrangBaiviet(bv.mabaiviet, ttbvBll.UnBlock());
        Response.Redirect("Phumuc.aspx?macm=" + bv.maphumuc);
    }

    protected void link_print_Click(object sender, EventArgs e)
    {
        Response.Redirect("PrintBaiviet.aspx?mabv=" + Int32.Parse(Page.Request["mabv"]));
    }
}
